﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Com;
using Core;
using Models;

namespace Controllers
{
    public class LinksController : AdminBaseController
    {
        public ActionResult GetPageList(pageinfo pageinfo)
        {
            if (!HasQx("links"))
            {
                return Error("没有权限！");
            }
            var core = new linksCore();
            List<object> argsList = new List<object>();
            string where = string.Empty;
            //where += " and endtime>=now()";
            if (!string.IsNullOrEmpty(pageinfo.key))
            {
                where += " and title like @" + argsList.Count + "";
                argsList.Add($"%{pageinfo.key}%");
            }

            switch (pageinfo.type)
            {
                case 1:
                    where += " and ismobile=0";
                    break;
                case 2:
                    where += " and ismobile=1";
                    break;
            }
            var list = core.GetPageList(pageinfo.page,pageinfo.pagesize, where, pageinfo.order+" "+pageinfo.esc, argsList.ToArray());
            return Success(list);
        }

        [HttpPost]
        public ActionResult SaveOrdnum(List<linksEntity> linkslist)
        {
            if (!HasQx("links"))
            {
                return Error("没有权限！");
            }
            var core = new linksCore();
            foreach (var linksEntity in linkslist)
            {
                core.Update(new { linksEntity.ordnum }, linksEntity.ID);
            }
            return Success();
        }
        [HttpPost]
        public ActionResult Del(int id)
        {
            if (!HasQx("links"))
            {
                return Error("没有权限！");
            }
            if (id > 0)
            {
                if (new linksCore().Del(id))
                {
                    Log(curmanage.username + "删除友情链接ID=" + id);
                    return Success("删除成功！");
                }

            }
            return Error("删除失败！");
        }
        [HttpPost]
        public ActionResult DelAll(List<linksEntity> modelList)
        {
            if (!HasQx("links"))
            {
                return Error("没有权限！");
            }
            var core = new linksCore();
            string ids = string.Empty;
            foreach (var linksEntity in modelList)
            {
                core.Del(linksEntity);
                ids += linksEntity.ID + ",";
            }
            Log(curmanage.username + "删除友情链接ID in (" + ids + ")");
            return Success("删除成功！");
        }
        [HttpPost]
        public ActionResult Save(linksEntity links)
        {
            if (!HasQx("links"))
            {
                return Error("没有权限！");
            }
            if (links.title == null)
            {
                return Error();
            }

            if (links.ID == 0)
            {
                links.addtime=DateTime.Now;
                links.sid = 1;//TODO 多站点调整
                links.mid = curmanage.ID;
            }
            var model = new linksCore().Save(links);
            Log(curmanage.username + "保存友情链接" + model.ToJson());
            return Success(model);
        }
    }
}
